
// 代码 12-6
pub struct MinStack{
    stack: Vec<i32>,
    min_stack: Vec<i32>,
}

 impl MinStack{
    pub fn new() -> Self
    {
        MinStack{
            stack: Vec::new(),
            min_stack: Vec::new()
        }
    }

    pub fn push(&mut self, x: i32)
    {
        self.stack.push(x);

        if self.min_stack.is_empty() ||
          x <= *self.min_stack.last().unwrap()
        {
            self.min_stack.push(x);
        }
    }


    pub fn pop(&mut self)
    {
        if  self.stack.is_empty() {
            return;
        }

        if self.stack.pop().unwrap() == *self.min_stack.last().unwrap(){
            self.min_stack.pop();
        }
    }

    pub fn top(&self) -> i32 {
        return *self.stack.last().unwrap();
    }

    pub fn get_min(&self) -> i32
    {
        *self.stack.last().unwrap()
    }

}

// 代码12-7
pub fn parenthese_is_vaild(str:String) -> bool
{
    /* 使用 chars的方法来切分字符串，并用收集器收集，注意声明接受的变量需要手动指明类型 */
    let chars: Vec<char> = str.chars().collect();

    if chars.len() == 0{
        return true;
    }

    let mut stack = Vec::new();

    for i in 0..chars.len()
    {
        if chars[i] == '('{
            stack.push(')');
        }
        else if chars[i] == '['{
            stack.push(']');
        }
        else if chars[i] == '{'{
            stack.push('}');
        }
        else if stack.is_empty() || chars[i] != stack.pop().unwrap(){
            return  false;
        }
    }

    return stack.is_empty();


}












